package com.tianyadu.security.core.validate.code.sms;

import com.tianyadu.security.core.properties.propertieVO.SecurityConstants;
import com.tianyadu.security.core.validate.code.ValidateCode;
import com.tianyadu.security.core.validate.code.service.impl.AbstractValidateCodeProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.context.request.ServletWebRequest;

/**
 * @FileName: SmsCodeProcessor
 * @Description:
 * @author: xueyj
 * @create: 2018-12-13 16:54
 */
@Component("smsValidateCodeProcessor")
public class SmsCodeProcessor extends AbstractValidateCodeProcessor<ValidateCode> {

    /**
     * 短信验证码发送器
     */
    @Autowired
    private SmsCodeSender smsCodeSender;

    /**
     * 发送校验码，由子类实现
     *
     * @param request
     * @param validateCode
     * @throws Exception
     */
    @Override
    protected void send(ServletWebRequest request, ValidateCode validateCode) throws Exception {
        //  接收短信验证码参数名
        String paramName = SecurityConstants.DEFAULT_PARAMETER_NAME_MOBILE;
        String mobile = ServletRequestUtils.getRequiredStringParameter(request.getRequest(), paramName);
        smsCodeSender.sendSms(mobile, validateCode.getCode());
    }
}